
import random
from torch.utils.data import Dataset, DataLoader
import numpy as np
from scipy.io import loadmat
import torch


def xcorr(x, y):
    maxlag = np.maximum(np.size(x), np.size(y)) - 1
    c = crosscorr(x, y, maxlag)
    return c


def load_DL(dir_path):
    DL_rff_dict = {}
    for i in range(1, 14):
        mat_path = dir_path + f'rff_{i}.mat'
        data = loadmat(mat_path)['rff_DL_overlay']
        DL_rff_dict[i] = data
    return DL_rff_dict


def crosscorr(x, y, maxlag):
    # Compute cross-correlation for vector inputs. Output is clipped based on
    # maxlag but not padded if maxlag >= max(size(x,1),size(y,1)).
    nx = np.size(x)
    ny = np.size(y)
    m = np.maximum(nx, ny)
    maxlagDefault = m - 1
    mxl = np.maximum(maxlag, maxlagDefault)

    m2 = findTransformLength(m)
    X = np.fft.fft(x, m2, 0)
    Y = np.fft.fft(y, m2, 0)
    c1 = np.fft.ifft(X * np.conj(Y), n=None, axis=0)
    # Keep only the lags we want and move negative lags before positive
    # lags.
    c = np.hstack((c1[m2 - mxl + np.arange(mxl)], c1[0:mxl + 1]))
    return c


def findTransformLength(mm):
    m = 2 * mm
    while True:
        r = m
        for p in [2, 3, 5, 7]:
            while (r > 1) and (r % p == 0):
                r = r / p
        if r == 1:
            break
        m = m + 1
    return m


def overlay_list(overlay_num, data):
    out_data = []
    for idx in range(len(data) - overlay_num + 1):
        overlay_rff = np.mean(data[idx:idx + overlay_num], axis=0)
        # overlay_rff=overlay_rff[:-1]-overlay_rff[1:]
        out_data.append(overlay_rff)

    return np.array(out_data)


import numpy as np
from scipy.io import loadmat


def load_mat(file_path, key_name, id_list=[]):
    '''
    读取文件id_list中对应id的数据
    :param file_path:
    :param key_name:
    :param id_list:
    :return:
    '''
    data = loadmat(file_path)[key_name]
    y_rff = np.real(data[:, -1])
    print(np.unique(y_rff))
    if len(id_list) != 0:
        keep = id_list
        mask = np.isin(y_rff, keep)
        data = data[mask]
    # np.random.shuffle(data)
    return data


def divide_data(x_data, y_data, dev_ratio=0.2, overlay_num=40):
    id_list = np.unique(y_data)
    len_id = []
    for id in id_list:
        y_data_id = y_data[y_data == id]
        len_id.append(len(y_data_id))
    min_len = min(len_id)
    train_size = int(min_len * (1 - dev_ratio))
    data_dict = []
    label = 0
    x_train = []
    y_train = []
    x_dev = []
    y_dev = []
    # x_data = np.log(x_data)
    overlay_num = overlay_num
    for y in id_list:
        _x = x_data[y_data == y]

        _x_train = _x[:train_size]
        _x_dev = _x[train_size:min_len]

        _x_overlay_train = iq_nn_inputs(overlay_list(overlay_num, _x_train))
        _y_overlay_train = np.full((len(_x_overlay_train),), y)

        _x_overlay_dev = iq_nn_inputs(overlay_list(overlay_num, _x_dev))
        _y_overlay_dev = np.full((len(_x_overlay_dev),), y)
        if len(x_train) != 0:
            x_train = np.concatenate((x_train, _x_overlay_train), axis=0)
            y_train = np.concatenate((y_train, _y_overlay_train), axis=0)
            x_dev = np.concatenate((x_dev, _x_overlay_dev), axis=0)
            y_dev = np.concatenate((y_dev, _y_overlay_dev), axis=0)
        else:
            x_train = _x_overlay_train
            y_train = _y_overlay_train
            x_dev = _x_overlay_dev
            y_dev = _y_overlay_dev
    return x_train, y_train, x_dev, y_dev, id_list


def get_domain_data(domain_load_list, overlay_num=40):
    # domain_load_list=[(file_path, key_name, id_list)]
    domain_data = []
    print(domain_load_list[0])
    for idx, (file_path, key_name, id_list, domain_sample) in enumerate(domain_load_list):
        data = load_mat(file_path, key_name, id_list)
        if len(domain_data) == 0:
            domain_data = data
        else:
            domain_data = np.concatenate((domain_data, data), axis=0)
    x_train, y_train, x_dev, y_dev, id_list = divide_data(domain_data[:, :-1], domain_data[:, -1], dev_ratio=0.2,
                                                          overlay_num=overlay_num)
    return (x_train, y_train, x_dev, y_dev, id_list, domain_sample)


def iq_nn_inputs(data_raw):
    data_in = np.array([data_raw])
    data_in = data_raw
    data_out = np.zeros((data_in.shape[0], 1, 2, data_in.shape[1]), dtype=np.float32)
    for i in range(len(data_in)):
        data_out[i, 0, 0, :] = np.real(data_in[i])
        data_out[i, 0, 1, :] = np.imag(data_in[i])
    return data_out


def divide_data(x_data, y_data, dev_ratio=0.2, overlay_num=40):
    id_list = np.unique(y_data)
    len_id = []
    for id in id_list:
        y_data_id = y_data[y_data == id]
        len_id.append(len(y_data_id))
    min_len = min(len_id)
    train_size = int(min_len * (1 - dev_ratio))
    data_dict = []
    label = 0
    x_train = []
    y_train = []
    x_dev = []
    y_dev = []
    # x_data = np.log(x_data)
    overlay_num = overlay_num
    for y in id_list:
        _x = x_data[y_data == y]

        _x_train = _x[:train_size]
        _x_dev = _x[train_size:min_len]

        _x_overlay_train = iq_nn_inputs(overlay_list(overlay_num, _x_train))
        _y_overlay_train = np.full((len(_x_overlay_train),), y)

        _x_overlay_dev = iq_nn_inputs(overlay_list(overlay_num, _x_dev))
        _y_overlay_dev = np.full((len(_x_overlay_dev),), y)
        if len(x_train) != 0:
            x_train = np.concatenate((x_train, _x_overlay_train), axis=0)
            y_train = np.concatenate((y_train, _y_overlay_train), axis=0)
            x_dev = np.concatenate((x_dev, _x_overlay_dev), axis=0)
            y_dev = np.concatenate((y_dev, _y_overlay_dev), axis=0)
        else:
            x_train = _x_overlay_train
            y_train = _y_overlay_train
            x_dev = _x_overlay_dev
            y_dev = _y_overlay_dev
    return x_train, y_train, x_dev, y_dev, id_list


def get_domain_data(domain_load_list, overlay_num=40):
    # domain_load_list=[(file_path, key_name, id_list)]
    domain_data = []
    print(domain_load_list[0])
    for idx, (file_path, key_name, id_list, domain_sample) in enumerate(domain_load_list):
        data = load_mat(file_path, key_name, id_list)
        if len(domain_data) == 0:
            domain_data = data
        else:
            domain_data = np.concatenate((domain_data, data), axis=0)
    x_train, y_train, x_dev, y_dev, id_list = divide_data(domain_data[:, :-1], domain_data[:, -1], dev_ratio=0.2,
                                                          overlay_num=overlay_num)
    return (x_train, y_train, x_dev, y_dev, id_list, domain_sample)


def sample_positive(x_data, y_data, id, sample_num=5):
    x_positive = x_data[y_data == id]
    sample_num = min(len(x_positive), sample_num)
    indexList = random.sample(range(len(x_positive)), sample_num)
    return x_positive[indexList]


def sample_negative(x_data, y_data, id, sample_num=5):
    x_negative = x_data[y_data != id]
    sample_num = min(len(x_negative), sample_num)
    indexList = random.sample(range(len(x_negative)), sample_num)
    return x_negative[indexList]


def xorr_with_local(rff, DL_rff_dict, id):
    id = np.real(id)
    indexList = random.sample(range(len(DL_rff_dict[id])), 1)
    return xcorr(rff[0:300], DL_rff_dict[id][indexList[0]][0:300])


# 合法非法设备标签1~10合法，11~15非法
# def sample_domain_dict(domain_dict, positive_num=2, nagetive_num=2, dev=False):
#     data_pair = []
#     rff_label = []
#     domain_use = []
#     domain_label = []
#     legal_ids = list(range(1, 2))  # 合法设备 id 范围
#     illegal_ids = list(range(3,4))  # 非法设备 id 范围
#
#     for domain_id in domain_dict:
#         data_dict = domain_dict[domain_id]
#         x = data_dict['x']
#         y = data_dict['y']
#         domain_sample = data_dict['domain_sample']
#         id_list = np.unique(y)
#         for id in id_list:
#             x_source = x[y == id]
#             for target_domain_id in domain_dict:
#                 data_dict_target = domain_dict[target_domain_id]
#                 x_target = data_dict_target['x']
#                 y_target = data_dict_target['y']
#                 if domain_sample:
#                     domain_use_default = 1
#                 else:
#                     domain_use_default = 0
#                 x_n_target = x_target[y_target != id]
#                 x_p_target = x_target[y_target == id]
#                 y_n_target = y_target[y_target != id]
#                 y_p_target = y_target[y_target == id]
#
#                 same_domain = 0
#                 if target_domain_id == domain_id:
#                     same_domain = 1
#                     if dev == True:
#                         continue
#
#                 rff_id = 0
#                 for rff in x_source:
#                     # 处理合法设备样本
#                     if id in legal_ids:
#                         indexList = random.sample(range(len(x_p_target)), positive_num)
#                         for index in indexList:
#                             x_same_class = x_p_target[index]
#                             positive_sample = [rff, x_same_class]
#                             data_pair.append(positive_sample)
#                             rff_label.append([1, id, id, rff_id])  # 合法设备对标记为 1
#                             rff_id += 1
#                             domain_label.append([same_domain])
#                             domain_use.append(domain_use_default)
#                         indexList = random.sample(range(len(x_n_target)), nagetive_num)
#                         for index in indexList:
#                             x_other_class = x_n_target[index]
#                             if y_n_target[index] in illegal_ids:
#                                 negative_sample = [rff, x_other_class]
#                                 data_pair.append(negative_sample)
#                                 rff_label.append([0, y_n_target[index], id, rff_id])  # 合法与非法设备对标记为 0
#                                 rff_id += 1
#                                 domain_label.append([0])
#                                 domain_use.append(0)
#                     # 处理非法设备样本
#                     elif id in illegal_ids:
#                         indexList = random.sample(range(len(x_n_target)), nagetive_num)
#                         for index in indexList:
#                             x_other_class = x_n_target[index]
#                             negative_sample = [rff, x_other_class]
#                             data_pair.append(negative_sample)
#                             rff_label.append([0, y_n_target[index], id, rff_id])  # 非法设备对标记为 0
#                             rff_id += 1
#                             domain_label.append([0])
#                             domain_use.append(0)
#
#     return data_pair, rff_label, domain_label, domain_use

# 对照观察设置1~10非法，11~15合法
# def sample_domain_dict(domain_dict, positive_num=2, nagetive_num=2, dev=False):
#     data_pair = []
#     rff_label = []
#     domain_use = []
#     domain_label = []
#     legal_ids = list(range(11, 16))  # 合法设备 id 范围
#     illegal_ids = list(range(1, 11))  # 非法设备 id 范围
#
#     for domain_id in domain_dict:
#         data_dict = domain_dict[domain_id]
#         x = data_dict['x']
#         y = data_dict['y']
#         domain_sample = data_dict['domain_sample']
#         id_list = np.unique(y)
#         for id in id_list:
#             x_source = x[y == id]
#             for target_domain_id in domain_dict:
#                 data_dict_target = domain_dict[target_domain_id]
#                 x_target = data_dict_target['x']
#                 y_target = data_dict_target['y']
#                 if domain_sample:
#                     domain_use_default = 1
#                 else:
#                     domain_use_default = 0
#                 x_n_target = x_target[y_target != id]
#                 x_p_target = x_target[y_target == id]
#                 y_n_target = y_target[y_target != id]
#                 y_p_target = y_target[y_target == id]
#
#                 same_domain = 0
#                 if target_domain_id == domain_id:
#                     same_domain = 1
#                     if dev == True:
#                         continue
#
#                 rff_id = 0
#                 for rff in x_source:
#                     # 处理合法设备样本
#                     if id in legal_ids:
#                         indexList = random.sample(range(len(x_p_target)), positive_num)
#                         for index in indexList:
#                             x_same_class = x_p_target[index]
#                             positive_sample = [rff, x_same_class]
#                             data_pair.append(positive_sample)
#                             rff_label.append([1, id, id, rff_id])  # 合法设备对标记为 1
#                             rff_id += 1
#                             domain_label.append([same_domain])
#                             domain_use.append(domain_use_default)
#                         indexList = random.sample(range(len(x_n_target)), nagetive_num)
#                         for index in indexList:
#                             x_other_class = x_n_target[index]
#                             if y_n_target[index] in illegal_ids:
#                                 negative_sample = [rff, x_other_class]
#                                 data_pair.append(negative_sample)
#                                 rff_label.append([0, y_n_target[index], id, rff_id])  # 合法与非法设备对标记为 0
#                                 rff_id += 1
#                                 domain_label.append([0])
#                                 domain_use.append(0)
#                     # 处理非法设备样本
#                     elif id in illegal_ids:
#                         indexList = random.sample(range(len(x_n_target)), nagetive_num)
#                         for index in indexList:
#                             x_other_class = x_n_target[index]
#                             negative_sample = [rff, x_other_class]
#                             data_pair.append(negative_sample)
#                             rff_label.append([0, y_n_target[index], id, rff_id])  # 非法设备对标记为 0
#                             rff_id += 1
#                             domain_label.append([0])
#                             domain_use.append(0)
#
#     return data_pair, rff_label, domain_label, domain_use


# 二分类标签，正负样本对，正样本对1，负样本对0
import random
import numpy as np

def sample_domain_dict(domain_dict, positive_num=2, nagetive_num=2, dev=False):
    # DL_rff_dict=load_DL('../data/rff/DL_overlay_2/')
    data_pair = []
    rff_label = []
    domain_use = []
    domain_label = []
    for domain_id in domain_dict:
        # 对域中每一个rff，遍历每个域采样样本对
        data_dict = domain_dict[domain_id]
        x = data_dict['x']
        y = data_dict['y']
        domain_sample = data_dict['domain_sample']
        id_list = np.unique(y)
        for id in id_list:
            x_source = x[y == id]
            for target_domain_id in domain_dict:
                data_dict_target = domain_dict[target_domain_id]
                x_target = data_dict_target['x']
                y_target = data_dict_target['y']
                if domain_sample:
                    domain_use_default = 1
                else:
                    domain_use_default = 0
                x_n_target = x_target[y_target != id]
                x_p_target = x_target[y_target == id]
                y_n_target = y_target[y_target != id]
                y_p_target = y_target[y_target == id]

                same_domain = 0
                if target_domain_id == domain_id:
                    same_domain = 1
                    if dev == True:
                        continue

                rff_id = 0
                for rff in x_source:
                    # 检查 positive_num 是否合法
                    valid_positive_num = min(positive_num, len(x_p_target))
                    valid_positive_num = max(valid_positive_num, 0)
                    indexList = random.sample(range(len(x_p_target)), valid_positive_num)
                    # print("-------------------------",rff.shape)
                    # rff=iq_nn_inputs(xorr_with_local(rff,DL_rff_dict,id))[0]
                    # print("-------------------------",rff.shape)

                    for index in indexList:
                        x_same_class = x_p_target[index]
                        # x_same_class=iq_nn_inputs(xorr_with_local(x_same_class,DL_rff_dict,id))[0]
                        # print("-------------------------",x_same_class.shape)
                        positive_sample = [rff, x_same_class]
                        data_pair.append(positive_sample)
                        rff_label.append([1, id, id, rff_id])
                        rff_id += 1
                        domain_label.append([same_domain])
                        domain_use.append(domain_use_default)
                    # 检查 nagetive_num 是否合法
                    valid_nagetive_num = min(nagetive_num, len(x_n_target))
                    valid_nagetive_num = max(valid_nagetive_num, 0)
                    indexList = random.sample(range(len(x_n_target)), valid_nagetive_num)
                    for index in indexList:
                        x_other_class = x_n_target[index]
                        # x_other_class=iq_nn_inputs(xorr_with_local(x_other_class,DL_rff_dict,y_n_target[index]))[0]
                        # print("-------------------------",x_same_class.shape)
                        negative_sample = [rff, x_other_class]
                        data_pair.append(negative_sample)

                        rff_label.append([0, y_n_target[index], id, rff_id])
                        rff_id += 1
                        domain_label.append([0])
                        domain_use.append(0)
    return data_pair, rff_label, domain_label, domain_use


def add_columns(original_array, n):
    num_rows = original_array.shape[0]
    fixed_column = np.full((num_rows, 1), n, dtype=original_array.dtype)

    result_array = np.hstack((original_array, fixed_column))
    return result_array


def load_data(data_list=[], overlay_num=40, dev=False):
    domain_train_dict = {}
    domain_dev_dict = {}
    for domain_id, domain_load_list in enumerate(data_list):
        (x_train_domain, y_train_domain, x_dev_domain, y_dev_domain, id_list, domain_sample) = get_domain_data(
            domain_load_list, overlay_num=overlay_num)
        # x_train_domain=add_columns(x_train_domain,domain_id)
        data_train_dict = {
            'x': x_train_domain, 'y': y_train_domain, 'domain_sample': domain_sample
        }
        data_dev_dict = {
            'x': x_dev_domain, 'y': y_dev_domain, 'domain_sample': domain_sample
        }
        domain_train_dict[domain_id] = data_train_dict
        domain_dev_dict[domain_id] = data_dev_dict
    train_data_pair, train_rff_label, train_domain_label, train_domain_use = sample_domain_dict(domain_train_dict)
    dev_data_pair, dev_rff_label, dev_domain_label, dev_domain_use = sample_domain_dict(domain_dev_dict, dev=dev)
    print('lennnnnnnnnn', len(dev_data_pair))
    return train_data_pair, train_rff_label, train_domain_label, train_domain_use, dev_data_pair, dev_rff_label, dev_domain_label, dev_domain_use, id_list



class MyDataset(Dataset):
    def __init__(self, x, y_label, y_domain, domain_use):
        self.data = x
        self.y_label = y_label
        self.y_domain = y_domain
        self.domain_use = domain_use

    def __getitem__(self, idx):
        if self.y_label is None:
            return self.data[idx]

        return self.data[idx][0], self.data[idx][1], torch.tensor(self.y_label[idx], dtype=torch.float32), torch.tensor(
            self.y_domain[idx], dtype=torch.float32), torch.tensor(self.domain_use[idx], dtype=torch.float32)

    def __len__(self):
        return len(self.data)


# path1 = [('D:/files/learning/LTE/rff/1017_rff_divide.mat', 'rff', [])]
# path2 = [('D:/files/learning/LTE/rff/1018_rff_divide.mat', 'rff', [])]

# # [(file_path, key_name, id_list)]
# train_data_pair, train_rff_label, train_domain_label, dev_data_pair, dev_rff_label, dev_domain_label = load_data(
#     data_list=[path2, path1])
# train_dataset = MyDataset(train_data_pair, train_rff_label, train_domain_label)
# train_dataloader = DataLoader(train_dataset, batch_size=512, drop_last=True)

# for idx,(x_1, x_2, label_list_y, domain_list_y )in enumerate(train_dataloader):
#     print(x_1.shape)
#         for _id in range(len(x_1)):
# if(label_list_y[_id]==0):
#     if x_1[_id,0,0,-1]==x_2[_id,0,0,-1]:
#         print('error1')
# else:
#     if x_1[_id,0,0,-1]!=x_2[_id,0,0,-1]:
#         print('error2')
# if (domain_list_y[_id] != 0):
#     if x_1[_id, -1] != x_2[_id, -1]:
#         print('error3')
# else:
#     if x_1[_id, -1] == x_2[_id, -1] and x_1[_id,-2]==x_2[_id,-2]:
#         print('error4')

# load_dataset(domain_load_list=[(path, 'rff_dmrs_half_divid', [])])
def testAdd(label_pred_list, label_list, valid_list, all_list, test_dict={}, train_list=[], invalid_list=[]):
    set1 = set(all_list)
    set2 = set(valid_list)
    set3 = set(train_list)
    diff_set = set1 - set3  # 或者使用 set1.difference(set2)
    invalid_list = invalid_list
    if len(test_dict) == 0:
        for valid_idx in valid_list:
            test_dict[valid_idx] = {}
            test_dict[valid_idx][valid_idx] = {}
            for invalid_idx in invalid_list:
                test_dict[valid_idx][invalid_idx] = {}
    for i in range(len(label_pred_list)):
        label_pred = label_pred_list[i][0]
        label = int(label_list[i][0])
        class_target_idx = int(label_list[i][2])
        class_input_idx = int(label_list[i][1])
        rff_id = int(label_list[i][3])

        correct = 1 if label_pred == label else 0
        if class_target_idx in valid_list:
            # print(test_dict)
            if (rff_id in test_dict[class_target_idx][class_input_idx]):
                test_dict[class_target_idx][class_input_idx][rff_id].append(correct)
            else:
                test_dict[class_target_idx][class_input_idx][rff_id] = [correct]
    return test_dict


def getTestOutInfo(label_pred_list, label_list, id_list, test_dict):
    if len(test_dict) == 0:
        for id in id_list:
            test_dict[id] = {}
            for target_idx in id_list:
                test_dict[id][target_idx] = []
    for i in range(len(label_pred_list)):
        label_pred = label_pred_list[i][0]
        label = int(label_list[i][0])
        class_target_idx = int(label_list[i][2])
        class_input_idx = int(label_list[i][1])
        # rff_id = int(label_list[i][3])

        correct = 1 if label_pred == label else 0
        test_dict[class_target_idx][class_input_idx].append(correct)
    return test_dict


def printTestOut(test_dict, save_path="out_movie_csi_time_domain.txt"):
    out_list = []
    valid_acc_list = []
    invalid_acc_list = []
    for target_id in test_dict:
        id_dict = test_dict[target_id]
        print('target: ', target_id)
        out_device = []
        invalid_acc = []
        for input_id in id_dict:
            corr_list = id_dict[input_id]
            # 检查 corr_list 是否为空
            if len(corr_list) == 0:
                acc = 1  # 当 corr_list 为空时，将 acc 设置为 0
                print(f"Warning: corr_list for input_id {input_id} is empty.")
            else:
                acc = sum(corr_list) / len(corr_list)
            if input_id == target_id:
                valid_acc = acc
            else:
                invalid_acc.append(acc)
            out_device.append(acc)
            print('input: {} acc:{:.4f} '.format(input_id, acc), end=' ')
        # 检查 invalid_acc 是否为空
        if len(invalid_acc) == 0:
            id_invalid_acc = 0  # 当 invalid_acc 为空时，将 id_invalid_acc 设置为 0
            print(f"Warning: invalid_acc list for target_id {target_id} is empty.")
        else:
            id_invalid_acc = sum(invalid_acc) / len(invalid_acc)
        print(' ')
        print('valid acc:{:.4f} invalid acc:{:.4f}'.format(valid_acc, id_invalid_acc))
        valid_acc_list.append(valid_acc)
        invalid_acc_list.append(id_invalid_acc)
        out_list.append(out_device)
    # 检查 valid_acc_list 是否为空
    if len(valid_acc_list) == 0:
        valid_all_acc = 0
        print("Warning: valid_acc_list is empty.")
    else:
        valid_all_acc = sum(valid_acc_list) / len(valid_acc_list)
    # 检查 invalid_acc_list 是否为空
    if len(invalid_acc_list) == 0:
        invalid_all_acc = 0
        print("Warning: invalid_acc_list is empty.")
    else:
        invalid_all_acc = sum(invalid_acc_list) / len(invalid_acc_list)
    mean_acc = (valid_all_acc + invalid_all_acc) / 2
    out_str = 'valid all acc:{:.4f} invalid all acc:{:.4f} mean acc:{:.4f}'.format(valid_all_acc, invalid_all_acc,
                                                                                   mean_acc)
    print(out_str)
    with open(save_path, "a") as file:
        file.write(out_str + "\n")
        file.write(str(out_list) + "\n")


def check_test_out(test_dict):
    vote_num = 1
    out_list = []
    for class_target_idx in test_dict:
        out_device = []
        print('target {}'.format(class_target_idx))
        target_out = test_dict[class_target_idx]
        for class_input_idx in target_out:
            corr_list = []
            for rff_id in target_out[class_input_idx]:
                if sum(test_dict[class_target_idx][class_input_idx][rff_id]) > vote_num / 2:
                    corr_list.append(1)
                else:
                    corr_list.append(0)
            if len(corr_list) > 0:
                out_device.append(sum(corr_list) / len(corr_list))
                # print('input {} acc {}'.format(class_input_idx,sum(corr_list)/len(corr_list)))
        out_list.append(out_device)

# path1 = [('../data/rff/1029_rff_log.mat', 'rff', [])]
# # path2 = [('../data/rff/1029_rff_log.mat', 'rff', id_list)]
# # [(file_path, key_name, id_list)]
# train_data_pair, train_rff_label, train_domain_label, dev_data_pair, dev_rff_label, dev_domain_label = load_data(
#     data_list=[ path1])
